package ysq.xyg.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.shop.dao.ShopBrandDao;
import ysq.xyg.shop.model.po.ShopBrand;
import ysq.xyg.shop.model.po.ShopItemCat;
import ysq.xyg.shop.model.vo.ShopItemCatVo;
import ysq.xyg.shop.service.ShopItemCatService;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.shop.model.vo.ShopBrandVo;
import ysq.xyg.shop.service.ShopBrandService;

import javax.annotation.Resource;
import java.util.*;


/**
 * 商城模块-品牌service实现
 *
 * @author congcong tao
 * @date 2019-4-28 11:07:22
 */
@Service("shopBrandService")
public class ShopBrandServiceImpl extends BaseServiceImpl<ShopBrand> implements ShopBrandService {

    @Resource
    private ShopItemCatService shopItemCatService;
    @Resource
    private ShopBrandDao shopBrandDao;

    private ShopBrand shopBrand;

    @Autowired
    public void setBaseDao(ShopBrandDao shopBrandDao) {
        this.baseDao = shopBrandDao;
    }


    /**
     * 分页查询
     *
     * @param shopBrandVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:07:22
     */
    public ReturnEntity page(ShopBrandVo shopBrandVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopBrand> list = acceptList(shopBrandVo);
        PageInfo<ShopBrand> page=new PageInfo<ShopBrand>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param shopBrandVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:07:22
     */
    public ReturnEntity addOrUpdate(ShopBrandVo shopBrandVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopBrandVo.getId())){
            shopBrand=new ShopBrand(currentUser);
            isUpate=false;
        }else{
            shopBrand=selectByPrimaryKey(shopBrandVo.getId());
            if(ObjectUtil.isNull(shopBrand)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopBrand.setUpdateTime(new Date());
            shopBrand.setUpdateUser(currentUser.getId());
        }
        shopBrand.setCatId(shopBrandVo.getCatId());
        if (StringUtil.isNotEmpty(shopBrandVo.getCatId())) {
            ShopItemCat shopItemCat = shopItemCatService.selectByPrimaryKey(shopBrandVo.getCatId());
            if (shopItemCat != null) {
                shopBrand.setCatName(shopItemCat.getName());
            }
        }
        shopBrand.setName(shopBrandVo.getName());
        shopBrand.setImage(shopBrandVo.getImage());
        shopBrand.setDescription(shopBrandVo.getDescription());
        shopBrand.setOrderNum(shopBrandVo.getOrderNum());
        shopBrand.setStatus(shopBrandVo.getStatus());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(shopBrand);
        }else{
            num=insert(shopBrand);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:07:22
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        ShopBrand shopBrand=selectByPrimaryKey(id);
        if(shopBrand!=null){
            shopBrand.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            shopBrand.setUpdateTime(new Date());
            shopBrand.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(shopBrand);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param shopBrandVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-4-28 11:07:22
     */
    @Override
    public List<ShopBrand> acceptList(ShopBrandVo shopBrandVo){
        Example example = new Example(ShopBrand.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(shopBrandVo.getName())){
            criteria.andLike("name","%" + shopBrandVo.getName() + "%");
        }
        if(StringUtil.isNotEmpty(shopBrandVo.getCatName())){
            criteria.andLike("catName","%" + shopBrandVo.getCatName() + "%");
        }
        if(StringUtil.isNotEmpty(shopBrandVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",shopBrandVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return shopBrandDao.selectByExample(example);
    }

    @Override
    public ReturnEntity acceptSelect(String code) {
        List<ShopBrand> list = acceptList(new ShopBrandVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(ShopBrand entity : list){
            SysDictContent sysDictContent = new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }

    @Override
    public ReturnEntity acceptBrandAndItemCatSelect(String code) {
        List<ShopBrand> list = acceptList(new ShopBrandVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(ShopBrand entity : list){
            SysDictContent sysDictContent = new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }

        List<ShopItemCat> itemCatList = shopItemCatService.acceptList(new ShopItemCatVo());
        List<SysDictContent> sysDictContentListI = new ArrayList<SysDictContent>();
        for(ShopItemCat entity : itemCatList){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName());
            sysDictContent.setDictType("shop_item_cat_id");
            sysDictContent.setValue(entity.getId());
            sysDictContentListI.add(sysDictContent);
        }
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("shopBrand", sysDictContentList);
        map.put("shopItemCat", sysDictContentListI);
        return new ReturnEntity(map);
    }
}
